package cn.itsource.pethome.user.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.RedisUtils;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.user.domain.dto.UserDto;
import cn.itsource.pethome.user.service.IUserService;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 用户控制层
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 检查手机号是否已经被注册
     * @param param
     * @return
     */
    @PostMapping("/checkPhone")
    public AjaxResult regFormPhone(@RequestBody Map<String,String> param){
        //获取到手机号
        String phone = param.get("phone");
        //查询手机号，是否被注册
        return userService.checkPhone(phone);
    }

    /**
     * 通过手机号进行注册
     * @param userDto
     * @return
     */
    @PutMapping("/register")
    public AjaxResult registerUser(@RequestBody UserDto userDto){
        //前台用户被注册
        try {
            userService.registerUser(userDto);
            return new AjaxResult(true, "恭喜你，注册成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 获取到token作为key,用户为value
     * @param loginUser
     */
    public Map<String, Object> getTokenAndLoginUser(User loginUser){
        //将登录对象转换为json字符串
        String userJsonStr = JSON.toJSONString(loginUser);
        //获取一个token 令牌
        String token = UUID.randomUUID().toString();
        //将登录对象存储到redis中
        RedisUtils.INSTANCE.set(token, userJsonStr, 60 * 30);
        //创建map集合存储登录用户和token
        Map<String, Object> map = new HashMap<>();
        map.put("loginUser", loginUser);
        map.put("token", token);
        return map;
    }


    /**
     * 通过手机号发送code进行登录
     * @param userDto
     * @return
     */
    @PutMapping("/loginByCode")
    public AjaxResult loginByCode(@RequestBody UserDto userDto){
        try {
            //前台用户登录通过验证码
            User loginUser = userService.loginByCode(userDto.getPhone(),userDto.getCode());
            //调用获取到token作为key,用户为value方法
            Map<String, Object> map = getTokenAndLoginUser(loginUser);
            //返回结果
            return new AjaxResult().setResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * Map<String,String> param  可以存储前台传递过来的参数
     * @param user
     * @return
     */
    @PatchMapping("/login")
    public AjaxResult loginUser(@RequestBody User user){
        try {
            //登录验证，返回登录用户
            User loginUser = userService.loginUser(user.getUsername(),user.getPassword());
            //调用获取到token作为key,用户为value方法
            Map<String, Object> map = getTokenAndLoginUser(loginUser);
            return new AjaxResult().setResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }




}
